package cn.tedu.boot08.controller;

import cn.tedu.boot08.entity.Product;
import cn.tedu.boot08.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductController {
    @Autowired
    ProductMapper mapper;
    @RequestMapping("/insert")
    public void insert(@RequestBody Product product){
        System.out.println("product = " + product);
        mapper.insert(product);
    }
    @RequestMapping("/select")
    public List<Product> select(){
        //把服务器查询到的list集合直接响应给客户端
        //当返回值为List集合时SpringMVC框架会自动将List集合转成JSON格式的字符串
        //然后再将字符串响应个客户端
        return mapper.select();
    }
    @RequestMapping("/delete")
    public void delete(int id){
        mapper.deleteById(id);
    }

    @RequestMapping("/selectById")
    public Product selectById(int id){
        //当返回值为Product自定义对象类型时,SpringMVC会将对象转成JSON格式
        //的字符串然后再响应给客户端
        return mapper.selectById(id);
    }
    @RequestMapping("/update")
    public void update(@RequestBody Product product){
        mapper.update(product);
    }

}
